package top.xxyu.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import top.xxyu.SSHWindows;
import top.xxyu.core.ResultData;
import top.xxyu.dao.FileInfoDao;
import top.xxyu.dao.WindowsServerDao;
import top.xxyu.entity.FileInfo;
import top.xxyu.entity.WindowsServer;
import top.xxyu.service.WindowsServerService;

import java.io.File;
import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping(value = "/deployWindows")
public class DeloyWindowController {
    @Autowired
    private WindowsServerDao windowsServerDao;
    @Autowired
    private FileInfoDao fileInfoDao;

    @Autowired
    private WindowsServerService windowsServerService;


    @RequestMapping(value = "/getDeployWindowsList")
    public ResultData getDeployWindowsList(){
        ArrayList<Object> objects = new ArrayList<>();
        List<WindowsServer> all = windowsServerDao.findAll(Sort.by(Sort.Direction.ASC, "ip","tomcatName","tomcatPatch"));
        return ResultData.success(all);
    }

    @RequestMapping(value = "/saveDeployWindowsList")
    public ResultData saveDeployWindowsList(@RequestBody List<WindowsServer> datas){
        System.out.println(datas);
        for (WindowsServer win : datas){
            if(Objects.isNull(win.getId())){
                win.setId(UUID.randomUUID().toString());
            }
        }
        windowsServerDao.saveAll(datas);
        return ResultData.success();
    }

    @RequestMapping(value = "/uploadPatch")
    public ResultData uploadPatch(@RequestParam("file") MultipartFile file){
//        windowsServerService.updatePatch()
        fileInfoDao.deleteAll();
        String fileName = file.getOriginalFilename();
        String filePath = "c:\\data\\temp";
        File folder = new File(filePath);
        if(!folder.exists()){
            folder.mkdirs();
        }
        File dest = new File(filePath +"\\"+ fileName);
        FileInfo fileInfo = new FileInfo();
        String id = UUID.randomUUID().toString();
        fileInfo.setId(id);
        fileInfo.setName(fileName);
        fileInfo.setPath(dest.getPath());
        fileInfoDao.save(fileInfo);
        try {
            file.transferTo(dest);
            SSHWindows.unZipFiles(dest, dest.getParent()+File.separator);
        } catch (IOException e) {
            fileInfoDao.deleteById(id);
        }
        return ResultData.success();
    }


    @RequestMapping(value = "/getFiles")
    public ResultData getFiles(){
        List<FileInfo> all = fileInfoDao.findAll(Sort.by(Sort.Direction.ASC, "name"));
        return ResultData.success(all);
    }

    @RequestMapping(value = "/deleteFile")
    public ResultData deleteFile(String id){
        fileInfoDao.deleteById(id);
        return ResultData.success();
    }

    @RequestMapping(value = "/updatePatch")
    public ResultData updatePatch(@RequestBody List<WindowsServer> datas){
        windowsServerService.updatePatch(datas);
        return ResultData.success();
    }
    @RequestMapping(value = "/doStart")
    public ResultData doStart(@RequestBody List<WindowsServer> datas){
        windowsServerService.doStart(datas);
        return ResultData.success();
    }
    @RequestMapping(value = "/doStop")
    public ResultData doStop(@RequestBody List<WindowsServer> datas){
        windowsServerService.doStop(datas);
        return ResultData.success();
    }

    @RequestMapping(value = "/executeKill")
    public ResultData executeKill(@RequestBody List<WindowsServer> datas){
        windowsServerService.executeKill(datas.get(0));
        return ResultData.success();
    }

    @RequestMapping(value = "/queryStatus")
    public ResultData queryStatus(@RequestBody List<WindowsServer> datas){
        for (WindowsServer window: datas ) {
            windowsServerService.queryStatus(window);
        }
        return ResultData.success();
    }
}
